/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package src.services.deposit;

import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import src.domain.deposit.Bilyet;
import src.services.gl.TaxModel;
import src.util.Constanta;

/**
 *
 * @author jasuke
 */

public class BilyetsimModel implements java.io.Serializable {

    public BilyetsimModel() {
    }
    
    public Date setMoth(Date date, int monthIdx) {
        
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, monthIdx);
        
        return cal.getTime();
    } 
    
    public int getDayOftheMonth(Date date) {
        
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        
        return cal.getActualMaximum(Calendar.DAY_OF_MONTH);
    } 
                
    public List<Bilyet> simulation(Bilyet bilyet){
        
        List<Bilyet> bilyets = new ArrayList<>();
        bilyets.clear();
        
        BigDecimal percentDivisor = BigDecimal.valueOf(100);
        
        BigDecimal taxRate = new TaxModel().getById(bilyet.getDeproduct().getTaxId()).getTaxRate().divide(percentDivisor, 2, RoundingMode.HALF_DOWN);
        BigDecimal intRate = bilyet.getInterestRate().divide(percentDivisor,2, RoundingMode.HALF_UP);
                
        BigDecimal period = BigDecimal.valueOf(bilyet.getDeproduct().getNumOfPeriod())
                    .divide(BigDecimal.valueOf(30), 0, RoundingMode.UP);
        
        //total bunga 
        BigDecimal amount = bilyet.getPrincipleAmount().multiply(intRate);
        
        //bunga bulanan
        BigDecimal intmonth = amount.divide(period, 0, RoundingMode.CEILING);
        BigDecimal outstandingTax =  BigDecimal.ZERO;
        BigDecimal taxMonth =BigDecimal.ZERO;
        if (intmonth.compareTo(Constanta.MAX_INTEREST_TAXED) > 0) {
             outstandingTax =  amount.multiply(taxRate);            
            //taxMonth = intmonth.multiply(taxRate, new MathContext(0, RoundingMode.DOWN));
            taxMonth = intmonth.multiply(taxRate);//, new MathContext(0, RoundingMode.DOWN)).round(RoundingMode.DOWN);
            taxMonth= taxMonth.setScale(0, RoundingMode.DOWN);
        }
        
        BigDecimal outStandingInt = amount;
        for (int i=1; i<= period.intValue(); i++) {
        
            Date date = setMoth(bilyet.getBilyetDate(), i);
            Bilyet deposit = new Bilyet();
           
            if (i==period.intValue()){
                intmonth = outStandingInt;//amount.subtract(outStandingInt); 
                taxMonth = outstandingTax;
            }
            outStandingInt = outStandingInt.subtract(intmonth);
            outstandingTax = outstandingTax.subtract(taxMonth);
            deposit.setInterestAmount(intmonth);
            deposit.setTaxAmount(taxMonth);
            deposit.setBilyetDate(date);
            
            bilyets.add(deposit);
        }
        
        return bilyets;
    }
    
    
    public List<Bilyet> simulationHari(Bilyet bilyet){
        
        List<Bilyet> bilyets = new ArrayList<>();
        bilyets.clear();
        
        BigDecimal percentDivisor = BigDecimal.valueOf(100);
        
        BigDecimal taxRate = new TaxModel().getById(bilyet.getDeproduct().getTaxId()).getTaxRate().divide(percentDivisor, 2, RoundingMode.HALF_DOWN);
        BigDecimal intRate = bilyet.getInterestRate().divide(percentDivisor,2, RoundingMode.HALF_UP);
                
        BigDecimal period = BigDecimal.valueOf(bilyet.getDeproduct().getNumOfPeriod())
                    .divide(BigDecimal.valueOf(30), 0, RoundingMode.UP);
        
        //total bunga 
        BigDecimal amount = bilyet.getPrincipleAmount().multiply(intRate)
                .divide(BigDecimal.valueOf(Constanta.DAYS_OF_AYEAR), 2, RoundingMode.HALF_UP);
        
        
        BigDecimal outStandingInt = bilyet.getPrincipleAmount().multiply(intRate);
        for (int i=1; i<= period.intValue(); i++) {
        
            Date date = setMoth(bilyet.getBilyetDate(), i);
            Bilyet deposit = new Bilyet();
            deposit.setBilyetDate(date);
            
            int days = getDayOftheMonth(date);
            //MathContext mc = new MathContext(0, RoundingMode.HALF_DOWN);
            BigDecimal intmonth = amount.multiply(BigDecimal.valueOf(days));
            
            BigDecimal taxMonth =BigDecimal.ZERO;
            if (intmonth.compareTo(Constanta.MAX_INTEREST_TAXED) > 0) {
                taxMonth = intmonth.multiply(taxRate);
                taxMonth = taxMonth.setScale(0, RoundingMode.DOWN);
            } 

            if (i==period.intValue()){
                intmonth = outStandingInt;
            }
            outStandingInt = outStandingInt.subtract(intmonth);
            
            deposit.setTaxAmount(BigDecimal.ZERO);
            deposit.setInterestAmount(intmonth);
            deposit.setTaxAmount(taxMonth);
            bilyets.add(deposit);
        }
        
        return bilyets;
    }
    
    
}
